package com.qiangqiang.service;

import com.qiangqiang.aop.annotation.CheckHasDormitory;
import com.qiangqiang.aop.annotation.CheckHasStudent;
import com.qiangqiang.cache.RedisClient;
import com.qiangqiang.dto.StudentDTO;
import com.qiangqiang.entity.Dormitory;
import com.qiangqiang.entity.Student;
import com.qiangqiang.enums.DormitoryEnum;
import com.qiangqiang.enums.StudentEnum;
import com.qiangqiang.exception.DormitoryException;
import com.qiangqiang.exception.StudentException;
import com.qiangqiang.repository.DorStuReposity;
import com.qiangqiang.repository.DormitoryRepository;
import com.qiangqiang.repository.StudentRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by q on 2018/2/4.
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "student")
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private DorStuReposity dorStuReposity;

    @Autowired
    private DormitoryRepository dormitoryRepository;

    @Autowired
    private RedisClient redisClient;


    /**
     * 更新或添加一个学生
     *
     * @param student
     * @return
     */
    public Student addOrUpdateStudent(Student student) {
        //查询是否存在过该学生
        Student objStudent = studentRepository.queryBySId(student.getSId());
        if (objStudent == null) {
            //不存在的话就添加该学生
            return studentRepository.save(student);
        } else {
            //存在该学生就更新学生信息
            BeanUtils.copyProperties(student, objStudent);
            return studentRepository.save(objStudent);
        }
    }

    /**
     * 根据id删除一个学生
     *
     * aop 验证
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @CheckHasStudent
    public void deleteStudent(String id) {
        studentRepository.deleteBySId(id);
    }

    /**
     * 分页查询所有学生
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Student> findAllStudents(int page, int size) {
        //根据page,size 来分页
        PageRequest request = new PageRequest(page, size);
        return studentRepository.findAll(request);
    }

    /**
     * 根据学号查询一个学生
     *
     * @param id
     * @return
     */

    @CheckHasStudent
    @Cacheable(key = "'stu'+#id")
    public StudentDTO findById(String id) {
        //是否存在该学生
        Student student = studentRepository.queryBySId(id);
        //该学生有没被分配宿舍
        String dId = dorStuReposity.queryDIdBySId(id);
        //返回学生的信息，包含宿舍号
        StudentDTO studentDTO=new StudentDTO();
        //TODO 需要用AOP优化
        if (dId == null) {
            throw new StudentException(StudentEnum.SID_NOT_DORMITORY);
        }
        BeanUtils.copyProperties(student,studentDTO);
        studentDTO.setDId(dId);
        return studentDTO;
    }

    /**
     * 根据姓名查询学生信息
     *
     * @param name
     * @return
     */
    public StudentDTO findByName(String name) {
        //TODO
       return null;
    }

    /**
     * 根据宿舍号查询该宿舍所有学生
     * @param dId
     * @return
     */
    @CheckHasDormitory
    public List<Student> findByDid(String dId){
        String key="stu:"+dId;
        if(redisClient.isExistKey(key)){
            return (List<Student>) redisClient.getAllCacheData(key);
        }
        else {
            List<Student> list=studentRepository.queryByDid(dId);
            redisClient.pushAll(key,list);
            return list;
        }
    }

}
